require('dotenv').config()

const cors  = require('cors')
const morgan = require('morgan')

const initDB  = require('./src/init/initDB')
const initServer  = require('./src/init/initServer')
const initRoute  = require('./src/init/initRoute')

const noMatchMiddleware  =require('./src/middleware/noMatchMiddleware')
const errorMiddleware=require('./src/middleware/errorMiddleware')

const express = require('express')
const app = express()

//中间件
// 跨域
app.use(cors())

//数据解析
app.use(express.json())

// http请求日志
app.use (morgan('tiny')) 
// the minial output ;  最小限度的输出
//:method :url :status :res[content-length] - :response-time ms'  //设置默认


//路由模块化
initRoute(app)

// 静态服务
app.use(express.static('public'))

//404处理 : 自定义中间件
app.use(noMatchMiddleware)

//统一错误处理中间件
//01定义统一错误
//02统一处理错误
app.use(errorMiddleware)


// 先启动数据库服务 再启动接口服务
const main = async ()=>{
    await initDB()
    await initServer(app)
}

main()





